package com.yylending.las.splash;

import android.Manifest;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;

import com.tbruyelle.rxpermissions.Permission;
import com.tbruyelle.rxpermissions.RxPermissions;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.umeng.analytics.MobclickAgent;
import com.umeng.analytics.MobclickAgent.EScenarioType;
import com.yyfax.infrastructure.PermissionsGrantedHelper;
import com.yylending.las.BuildConfig;
import com.yylending.las.R;
import com.yylending.las.YJKD;
import com.yylending.las.common.utils.preferenceutils.PreferenceConstants;
import com.yylending.las.main.MainActivity;

/**
 * Created by Pengrf on 2016/11/17 16:56.
 */
public class SplashActivity extends RxAppCompatActivity {
	private final static String TAG = "SplashActivity";

	private final static int PERMISSION_REQUEST_CODE = 1001;

	private PermissionsGrantedHelper.PermissionsGrantedListener mGrantedListener = new PermissionsGrantedHelper.PermissionsGrantedListener() {
		@Override
		public void permissionsEachGrantedResult(Permission permission) {
			switch (permission.name) {
				case Manifest.permission.READ_PHONE_STATE:
					initMobClickAgent();
					jumpPage();
					/*if (permission.granted) {
						onSucceed(PERMISSION_REQUEST_CODE);
					} else {

					}*/
			}
		}

		@Override
		public void PermissionsAllGrantedResult(boolean granted, int requestCode) {

		}
	};

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		ImageView bg = new ImageView(this);
		bg.setScaleType(ImageView.ScaleType.FIT_XY);
		bg.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
		bg.setImageResource(R.drawable.splash_bg);
		setContentView(bg);

		AlphaAnimation alpha = new AlphaAnimation(0.4f, 1);
		alpha.setDuration(1000);
		bg.startAnimation(alpha);
		alpha.setAnimationListener(new Animation.AnimationListener() {
			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				//for umeng.com analytics sdk
/*				shouldShowRequestPermissionRationale(this, Manifest.permission.READ_PHONE_STATE)//当已被拒绝过一次，再次申请权限时可弹自定义对话框说明
				if (PermissionUtil.checkAppPermissionWithVersion(SplashActivity.this, Manifest.permission.READ_PHONE_STATE)) {
					onSucceed(PERMISSION_REQUEST_CODE);
				} else {
					AndPermission.with(SplashActivity.this)
							.requestCode(PERMISSION_REQUEST_CODE)
							.permission(Manifest.permission.READ_PHONE_STATE)
							.send();
				}*/
				RxPermissions rxPermissions = new RxPermissions(SplashActivity.this);
				PermissionsGrantedHelper.PermissionsEachGranted(rxPermissions, mGrantedListener, Manifest.permission.READ_PHONE_STATE,
						Manifest.permission.READ_SMS, Manifest.permission.READ_CALL_LOG, Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_COARSE_LOCATION);
			}
		});
	}

	private void initMobClickAgent() {
		MobclickAgent.setDebugMode(BuildConfig.DEBUG);
		MobclickAgent.openActivityDurationTrack(false);
		MobclickAgent.setScenarioType(this, EScenarioType.E_UM_NORMAL);
		MobclickAgent.onEvent(this, "initMobclickAgent");
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onPageStart("SplashActivity");
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd("SplashActivity");
		MobclickAgent.onPause(this);
	}

	private void jumpPage() {
		generateIsUpgrade();
		Boolean firstTime = YJKD.getInstance().getPreferenceManager().isFirstLaunch(false);//todo maybe the new version should show guide
		if (!firstTime) {
			GuideActivity.start(this, GuideActivity.MODE_SPLASH);
			finish();
		} else {
			MainActivity.start(this);
			finish();
		}
	}

	private boolean generateIsUpgrade(){
		boolean isUpgrade = false;
		//  取缓存的versionCode
		int cacheVersion = YJKD.getInstance().getPreferenceManager().getAppVersion(0);
		// 取当前app的versionCode
		int appVersion = BuildConfig.VERSION_CODE;
		// 二者作比较,如果当前app的versionCode大于缓存的， 则说明是升级安装, 退出登录，并且缓存新的版本号
		if (cacheVersion > 0 && appVersion > cacheVersion) {
			isUpgrade = true;
		}
		YJKD.getInstance().getPreferenceManager().savePreference(PreferenceConstants.SP_KEY_APP_VERSION, appVersion);

		return isUpgrade;
	}
}
